package com.ruoyi.outcome.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.outcome.domain.vo.ResearchPatentTrack;
import com.ruoyi.outcome.domain.vo.ResearchProductTrack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.outcome.domain.ResearchProduct;
import com.ruoyi.outcome.service.IResearchProductService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 产品管理Controller
 * 
 * @author zxh
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/product")
public class ResearchProductController extends BaseController
{
    @Autowired
    private IResearchProductService researchProductService;

    /**
     * 查询产品管理列表
     */
    @RequiresPermissions("outcome:product:list")
    @GetMapping("/list")
    public TableDataInfo list(ResearchProduct researchProduct)
    {
        startPage();
        researchProduct.setIsDeleted(0);
        List<ResearchProduct> list = researchProductService.selectResearchProductList(researchProduct);
        return getDataTable(list);
    }

    /**
     * 导出产品管理列表
     */
    @RequiresPermissions("outcome:product:export")
    @Log(title = "产品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ResearchProduct researchProduct)
    {
        List<ResearchProduct> list = researchProductService.selectResearchProductList(researchProduct);
        ExcelUtil<ResearchProduct> util = new ExcelUtil<ResearchProduct>(ResearchProduct.class);
        util.exportExcel(response, list, "产品管理数据");
    }

    /**
     * 获取产品管理详细信息
     */
    @RequiresPermissions("outcome:product:query")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return success(researchProductService.selectResearchProductByProductId(productId));
    }

    /**
     * 新增产品管理
     */
    @RequiresPermissions("outcome:product:add")
    @Log(title = "产品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResearchProduct researchProduct)
    {
        researchProduct.setCreatedBy(SecurityUtils.getUsername());

        ResearchProductTrack productTrack=new ResearchProductTrack();
        BeanUtils.copyProperties(researchProduct, productTrack);
        productTrack.setActionType("ADD");
        researchProductService.insertResearchProductAudit(productTrack);
        return toAjax(researchProductService.insertResearchProduct(researchProduct));
    }

    /**
     * 修改产品管理
     */
    @RequiresPermissions("outcome:product:edit")
    @Log(title = "产品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResearchProduct researchProduct)
    {
        researchProduct.setUpdatedBy(SecurityUtils.getUsername());

        researchProduct.setUpdatedAt(new Date());

        ResearchProductTrack productTrack=new ResearchProductTrack();
        BeanUtils.copyProperties(researchProduct, productTrack);
        productTrack.setActionType("UPDATE");
        researchProductService.insertResearchProductAudit(productTrack);
        return toAjax(researchProductService.updateResearchProduct(researchProduct));
    }

    /**
     * 删除产品管理
     */
    @RequiresPermissions("outcome:product:remove")
    @Log(title = "产品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        for(Long productId:productIds)
        {
            ResearchProduct researchProduct = researchProductService.selectResearchProductByProductId(productId);
            researchProduct.setDeletedBy(SecurityUtils.getUsername());
            researchProduct.setDeletedAt(new Date());
            researchProduct.setIsDeleted(1);
            researchProductService.updateResearchProduct(researchProduct);

            ResearchProductTrack productTrack=new ResearchProductTrack();
            BeanUtils.copyProperties(researchProduct, productTrack);
            productTrack.setActionType("DELETE");
            researchProductService.insertResearchProductAudit(productTrack);
        }
        return success();
       // return toAjax(researchProductService.deleteResearchProductByProductIds(productIds));
    }
    @GetMapping("/product/{productId}")
    public AjaxResult getauditInfo(@PathVariable("productId") Long productId)
    {
        List<ResearchProductTrack> list = researchProductService.selectResearchProductAuditList(productId);
        return success(list);
    }
}
